<template>
  <div>
    <div class="search-content">
      <el-form ref="searchForm" :inline="true" :model="searchContent">
        <el-form-item label="兑换时间：" prop="order_time">
          <el-date-picker
            v-model="searchContent.order_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="兑换单号：" prop="point_ordersn">
          <el-input
            class="search-content-input"
            v-model="searchContent.point_ordersn"
            placeholder="兑换单号"
          ></el-input>
        </el-form-item>
        <el-form-item label="会员名称：" prop="point_buyername">
          <el-input
            class="search-content-input"
            v-model="searchContent.point_buyername"
            placeholder="会员名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号：" prop="member_mobile">
          <el-input
            class="search-content-input"
            v-model="searchContent.member_mobile"
            placeholder="手机号"
          ></el-input>
        </el-form-item>
        <el-form-item label="关键字：" prop="point_names">
          <el-input
            class="search-content-input"
            v-model="searchContent.point_names"
            placeholder="关键字"
          ></el-input>
        </el-form-item>
        <el-form-item label="供应商：" prop="pgoods_business_txt">
          <el-input
            class="search-content-input"
            v-model="searchContent.pgoods_business_txt"
            placeholder="供应商"
          ></el-input>
        </el-form-item>
        <el-form-item label prop="category_id">
          <el-select
            clearable
            v-model="searchContent.category_id"
            placeholder="产品类目"
          >
            <el-option
              v-for="item in category_options"
              :key="item.category_id"
              :label="item.category_name"
              :value="item.category_id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label prop="point_paymentcode">
          <el-select
            clearable
            v-model="searchContent.point_paymentcode"
            placeholder="支付方式"
          >
            <el-option label="线下支付" value="offline"></el-option>
            <el-option label="支付宝" value="alipay"></el-option>
            <el-option label="财付通" value="tenpay"></el-option>
            <el-option label="网银在线" value="chinabank"></el-option>
            <el-option label="预存款" value="predeposit"></el-option>
            <el-option label="积分" value="point"></el-option>
            <el-option label="积分+现金" value="point_pay"></el-option>
            <el-option label="其他" value="other"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label prop="point_orderstate">
          <el-select
            clearable
            v-model="searchContent.point_orderstate"
            placeholder="状态"
          >
            <el-option
              v-for="item in type_options"
              :key="item.value_txt"
              :label="item.value_txt"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label prop="payed">
          <el-select
            clearable
            v-model="searchContent.payed"
            placeholder="现金已付"
          >
            <el-option value="0" label="否"></el-option>
            <el-option value="1" label="是"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label prop="is_bill">
          <el-select
            clearable
            v-model="searchContent.is_bill"
            placeholder="对账状态"
          >
            <el-option value="1" label="已对账"></el-option>
            <el-option value="0" label="未对账"></el-option>
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item label prop="is_review">
          <el-select
            clearable
            v-model="searchContent.is_review"
            placeholder="复核状态"
          >
            <el-option value="0" label="未复核"></el-option>
            <el-option value="1" label="已复核"></el-option>
          </el-select>
        </el-form-item> -->


        <el-form-item label prop="is_review">
          <el-select
          class="statusselect"
            v-model="status"
          >
            <el-option v-for="item in statusList" :key="item.value" :value="item.value" :label="item.label"></el-option>
          </el-select>
          <el-select
            clearable
            v-model="statusValue"
          >
          <span v-if="status == 0">
            <el-option value="1" label="已结算"></el-option>
            <el-option value="0" label="未结算"></el-option>
          </span>
          <span v-if="status == 1">
             <el-option
              v-for="item in type_options"
              :key="item.value_txt"
              :label="item.value_txt"
              :value="item.value"
            ></el-option>
          </span>
          <span v-if="status == 2">
            <el-option value="1" label="已对账"></el-option>
            <el-option value="0" label="未对账"></el-option>
          </span>
          <span v-if="status == 3">
            <el-option value="0" label="未复核"></el-option>
            <el-option value="1" label="已复核"></el-option>
          </span>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">搜索</el-button>
          <el-button type="primary" @click="resetSearchData">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="search-list">
      <el-table
        :data="goodsList"
        border
        :height="oHeight"
        :fit="true"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          align="center"
          type="selection"
          width="55"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_ordersn"
          label="兑换单号"
          min-width="160"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_names"
          label="商品"
          min-width="300"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_buyername"
          label="会员名称"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_pointnum"
          label="兑换元宝"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_paynum"
          label="现金支付"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_shippingfee"
          label="运费"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_comprice_all"
          label="订单成本"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="pgoods_business_txt"
          label="供应商"
          min-width="120"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_paymentname"
          label="支付方式"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="point_addtime"
          label="兑换时间"
          min-width="100"
          #default="{ row }"
          >{{
            (row.point_addtime * 1000) | dateFormatter("YYYY-MM-DD hh:mm:ss")
          }}</el-table-column
        >
        <el-table-column
          align="center"
          label="状态"
          min-width="130"
          #default="{ row }"
        >
          {{ row.point_orderstatetext.order_state }}
          <span v-if="row.point_orderstatetext.change_state">{{
            `,${row.point_orderstatetext.change_state}`
          }}</span>
        </el-table-column>
        <el-table-column
          align="center"
          prop="point_origin"
          label="来源"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="is_bill_desc"
          label="对账状态"
          min-width="80"
        ></el-table-column>
        <el-table-column
          align="center"
          prop="is_review_desc"
          label="复核状态"
          min-width="80"
        ></el-table-column>
        <el-table-column
          fixed="right"
          align="center"
          prop="isInitiative"
          label="操作"
          min-width="260"
          #default="{ row }"
        >
          <el-button
            v-if="row.is_review !== '1'"
            type="primary"
            @click="review(row)"
            >复核</el-button
          >
          <el-button
            v-if="row.is_review === '1'"
            type="primary"
            @click="billRollback(row)"
            >对账退回</el-button
          >
        </el-table-column>
      </el-table>
      <div class="bottom">
        <div class="btn-group">
          <el-button type="primary" class="btn-bottom" @click="expertData"
            >导出检索数据</el-button
          >
          <el-button type="primary" class="btn-bottom" @click="oneReview"
            >一键复核</el-button
          >
          <el-button type="primary" class="btn-bottom" @click="oneSupReview"
            >一键超级复核</el-button
          >
          <el-button type="primary" class="btn-bottom" @click="pointcalc"
            >成本计算</el-button
          >
          <!-- <el-button type="primary" class="btn-bottom" @click="oneReview(1)">一键回退</el-button> -->
        </div>
        <!--分页部分-->
        <div class="paginationOne">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="searchContent.currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="searchContent.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
        <el-dialog
          :visible.sync="pointVisible"
          width="400px"
          :before-close="handleClose"
        >
          <span>总成本: {{ total_point }}</span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>
<script>
import { resize } from "@/common/js/height.js";
import { dateFormatter, objScreen } from "@/common/js/util";
export default {
  data() {
    return {
      goodsList: [],
      oHeight: 800,
      searchContent: {
        currentPage: 1,
        pageSize: localStorage.getItem("pageSize") * 1 || 10,
        order_time: "",
        point_ordersn: "",
        point_buyername: "",
        member_mobile: "",
        point_names: "",
        pgoods_business_txt: "",
        category_id: "",
        point_paymentcode: "",
        point_orderstate: "",
        payed: "",
        is_review: "",
        is_bill:"",
      },
      searchObj: {},
      total: 0,
      category_options: [],
      type_options: [],
      pgoods_brand_options: [],
      check: [],
      total_point: "",
      pointVisible: false,
        statusList: [
            { label: "结算状态", value: '0' },
            { label: "订单状态", value: '1' },
            { label: "对账状态", value: '2' },
            { label: "复核状态", value: '3' }],
        status: '0',
      statusValue:""
    };
  },
  created() {
    this.getListData();
    this.beforeData();
    },
    watch: {
        status(newval,oldval) {
            console.log(newval, oldval);
            this.statusValue = "";
         }
    },
    methods: {
    pointcalc() {
      var data = this.searchObj;
      //已对账订单
      // data.is_bill = "1";
      data.page = this.searchContent.currentPage;
      data.pageSize = this.searchContent.pageSize;
      this.$http
        .get(
          "/admin/pointprod/index.php?act=pointOrder&op=calculateTotalCost",
          { params: data }
        )
        .then((res) => {
          this.total_point = res.data.total_cost;
          this.pointVisible = true;
        });
    },
    handleClose() {
      this.total_point = "";
      this.pointVisible = false;
    },
    beforeData() {
      var data = {
        page: 1,
        pageSize: 99999,
      };
      this.$http
        .get(
          `/admin/pointprod/index.php?act=pointCategory&op=list&parent_id=0`,
          {
            params: data,
          }
        )
        .then((res) => {
          this.category_options = res.data.list;
        })
        .catch((err) => {
          this._message("error", err);
        });
      this.$http
        .get(`/admin/pointprod/index.php?act=pointOrder&op=stateList`, {
          params: data,
        })
        .then((res) => {
          this.type_options = res.data;
        })
        .catch((err) => {
          this._message("error", err);
        });
    },
    oneReview(type) {
      let url = "/admin/bill/index.php?act=onlineBill&op=review";
      let idArr = [];
      if (type === 1) {
        url = "/admin/bill/index.php?act=onlineBill&op=billRollback";
      }
      if (this.check.length === 0) {
        this.$message.warning("请勾选数据");
        return;
      }
      this.check.forEach((item) => {
        idArr.push(item.point_orderid);
      });
      this.$http
        .post(url, {
          id: idArr,
          order_type: "1",
        })
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    oneSupReview() {
      if (this.total > 10000) {
        return this.$message.error('数据超10000条，请先筛选！')
      }
      var data = this.searchObj;
      data.order_type = 1;
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=superReview", data)
        .then((res) => {
          if (res.code === 1) {
            this.$message.success(res.msg);
            this.search();
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    handleSelectionChange(val) {
      this.check = val;
    },
    getListData() {
      console.log(this.searchObj,'his.searchObj');
      var data = this.searchObj;
      //已对账订单
      // data.is_bill = "1";
      data.page = this.searchContent.currentPage;
      data.pageSize = this.searchContent.pageSize;
      this.$http
        .get("/admin/pointprod/index.php?act=pointOrder&op=list", {
          params: data,
        })
        .then((res) => {
          if (res.code === 1) {
            this.total = res.data.totalCount;
            this.goodsList = res.data.list;
            // this._message('success', res.msg);
            // this.getListData();
          } else {
            this.total = 0;
            this.goodsList = [];

            this._message("error", res.msg);
          }
        })
        .catch((err) => {
          this._message("error", err);
        });
    },
    search() {
      this.searchContent.currentPage = 1;
      let order_time = this.searchContent.order_time;
      this.searchObj = objScreen({
        order_start_time:
          order_time[0] && parseInt(order_time[0].getTime() / 1000),
        order_end_time:
          order_time[1] && parseInt(order_time[1].getTime() / 1000),
        point_ordersn: this.searchContent.point_ordersn,
        point_buyername: this.searchContent.point_buyername,
        pgoods_name: this.searchContent.pgoods_name,
        pgoods_show: this.searchContent.pgoods_show,
        category_id: this.searchContent.category_id,
        pgoods_ifpay: this.searchContent.pgoods_ifpay,
        pgoods_brand: this.searchContent.pgoods_brand,
        pgoods_business_txt: this.searchContent.pgoods_business_txt,
        pgoods_travel: this.searchContent.pgoods_travel,
        pgoods_virtual: this.searchContent.pgoods_virtual,
        pgoods_serial: this.searchContent.pgoods_serial,
        point_names: this.searchContent.point_names,
        member_mobile: this.searchContent.member_mobile,
        point_paymentcode: this.searchContent.point_paymentcode,
        // point_orderstate: this.searchContent.point_orderstate,
        payed: this.searchContent.payed,
        is_settled:"",
        // is_review: this.searchContent.is_review,
        // is_bill: this.searchContent.is_bill,
      });
      const val = this.statusValue
      this.status == 0 ? this.searchObj.is_settled = val : this.status == 1 ? this.searchObj.point_orderstate = val : this.status == 2 ? this.searchObj.is_bill = val : this.searchObj.is_review = val
      this.getListData();
    },
    resetSearchData() {
        this.$refs["searchForm"].resetFields();
        this.statusValue = "";
        this.status = '0';
      this.search();
    },
    expertData() {
      var host;
      if (this.$store.state.isServer) {
        host = location.host.replace("admin", "api");
      } else {
        host = location.host + "/api";
      }
      var url =
        location.protocol +
        "//" +
        host +
        "/admin/pointprod/index.php?act=pointOrder&op=list&export=1";
      var searchData = this.searchObj;
      for (var key in searchData) {
        if (
          !(
            searchData[key] === null ||
            searchData[key] === "" ||
            searchData[key] === undefined ||
            key === "page" ||
            key === "pageSize" ||
            key === "currentPage"
          )
        ) {
          url += "&" + key + "=" + searchData[key];
        }
      }
      console.log(url);
      window.location.href = url;
    },
    handleSizeChange(pageSize) {
      localStorage.setItem("pageSize", pageSize);
      this.searchContent.pageSize = pageSize;
      this.searchContent.currentPage = 1;
      this.getListData();
    },
    handleCurrentChange(page) {
      this.searchContent.currentPage = page;
      this.getListData();
    },
    buttonDeal(type, row) {
      switch (type) {
        case "showOrder":
          this.$refs.orderInfo.show_dialog_fn(row);
          break;
        case "add":
          this.$refs.updateDrawer.show_dialog_fn(row, 1);
          break;
        case "update":
          this.$refs.updateDrawer.show_dialog_fn(row);
          break;
        case "cancel":
          this.cancel(row);
          break;
      }
    },
    cancel(row) {
      this.$http
        .post("/admin/pointprod/index.php?act=pointOrder&op=cancel", {
          orderId: row.point_orderid,
        })
        .then((res) => {
          if (res.code === 1) {
            row.point_orderstate = 2;
            this._message("success", res.msg);
          } else {
            this._message("error", res.msg);
          }
        })
        .catch((err) => {
          this._message("error", err);
        });
    },
    review(row) {
      console.log(row);
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=review", {
          id: row.point_orderid,
          order_type: "1",
        })
        .then((res) => {
          if (res.code === 1) {
            row.is_review = "1";
            row.is_review_desc = "已复核";
            this.$message.success(res.msg);
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
    billRollback(row) {
      this.$http
        .post("/admin/bill/index.php?act=onlineBill&op=billRollback", {
          id: row.point_orderid,
          order_type: "1",
        })
        .then((res) => {
          if (res.code === 1) {
            /* row.is_review='0';
					row.is_review_desc='未复核'; */
            let index = this.goodsList.findIndex((item) => {
              return item === row;
            });
            this.goodsList.splice(index, 1);
            this.$message.success(res.msg);
          } else {
            this.$message.warning(res.msg);
          }
        })
        .catch((err) => {
          this.$message.error(err);
        });
    },
  },
  mounted() {
    resize(this);
  },
};
</script>
<style lang="scss" scoped>
.bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.search-content-input {
  width: 200px;
}
.statusselect {
    width: 100px;
}
</style>